#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution
{
public:
    vector<int> inventoryManagement(vector<int>& stock, int cnt)
    {
        srand(time(nullptr));
        qsort(stock, 0, stock.size() - 1, cnt);
        return { stock.begin(),stock.begin() + cnt };
    }
    void qsort(vector<int>& nums, int left, int right, int cnt)
    {
        if (left >= right) return;
        int l = left - 1, r = right + 1, i = left;
        int key = nums[rand() % (right - left + 1) + left];
        while (i < r)
        {
            if (nums[i] < key) swap(nums[++l], nums[i++]);
            else if (nums[i] == key) i++;
            else swap(nums[--r], nums[i]);
        }

        // [left,l] [l+1,r-1] [r,right]
        int a = l - left + 1, b = r - 1 - l;
        if (a > cnt) qsort(nums, left, l, cnt);
        else if (a + b >= cnt) return;
        else qsort(nums, r, right, cnt - a - b);
    }
};

class Solution {
public:
    vector<int> inventoryManagement(vector<int>& stock, int cnt) {
        sort(stock.begin(), stock.end());
        vector<int> ret;
        for (int i = 0; i < cnt; i++)
        {
            ret.push_back(stock[i]);
        }
        return ret;
    }
};